#pragma once

#include "public/build.h"
#include "core/noncopy.h"

#include "messiah/resource/repo/repo_item.h"

#include "external/tinyxml/tinyxml2.h"

#include <string>
#include <map>

namespace renderit
{
	namespace messiah
	{
		class repository;

		class repo_loader
		{
		public:
			repo_loader(const std::string& path) : repo_dir_(path) 
			{
				std::replace(repo_dir_.begin(), repo_dir_.end(), '\\', '/');
				repo_name_ = get_repo_name(repo_dir_); 
			}
			
			~repo_loader();

		public:
			bool load_repo();
			bool create_repo();

			std::string get_repo_name(std::string repo_dir);

			uint32 collect_all_guids();
			uint32 remove_unused_files();

			const std::string& repo_dir() const { return repo_dir_; }
			const std::string& repo_name() const { return repo_name_; }

			/*
			 * save if dirty
			 *
			 */
			void set_dirty() { is_dirty_ = true; }
			bool save();

		public:
			bool copy_item(repo_loader * other, const std::string& guid);

		public:
			struct unused_file
			{
				std::string guid;
				std::string fpath;
				uint32 fsize;
			};
			std::vector<unused_file> not_collect_files_;
						
		private:
			tinyxml2::XMLDocument doc_;
			std::vector<repo_item *> items_;
			std::map<std::string, repo_item*> items_map_;
			std::string repo_dir_;
			std::string repo_name_;

			bool load_suc_;

			bool is_dirty_;

		private:
			friend class repository;
		};
	}
}
